﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System.Web.Routing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.Expressions;

// Using declarations added for XML population of drop down lists

using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


namespace SRS_CP
{

    public partial class List : System.Web.UI.Page
    {
        protected MetaTable table;

        protected void Page_Init(object sender, EventArgs e)
        {
            table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
            GridView1.SetMetaTable(table, table.GetColumnValuesFromRoute(Context));
            GridDataSource.EntityTypeFilter = table.EntityType.Name;

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Title = table.DisplayName;
            GridDataSource.Include = table.ForeignKeyColumnsNames;

            // Disable various options if the table is readonly

            if (table.IsReadOnly)
            {
                GridView1.Columns[0].Visible = false;
                GridView1.EnablePersistedSelection = false;
            }

            // This code is added to bind the dropdown lists

            // if (!IsPostBack)
            // {
            //     this.BindData();
            // }
        }

        // private void BindData()
        // {
            // string query = "SELECT top 10 * FROM Assignees";
            // SqlCommand cmd = new SqlCommand(query);
            // GridView1.DataSource = GetData(cmd);
        // }

        // private DataTable GetData(SqlCommand cmd)

        // {
            // string ConString = ConfigurationManager.ConnectionStrings["SRSRequests"].ConnectionString;
            // using (SqlConnection con = new SqlConnection(ConString))

            // {
                // using (SqlDataAdapter sda = new SqlDataAdapter())
                // {
                    // cmd.Connection = con;
                    // sda.SelectCommand = cmd;
                    // using (DataTable dt = new DataTable())
                    // {
                        // sda.Fill(dt);
                        // return dt;
                    // }
                // }
            // }
        // }

        // protected void EditAssignee(object sender, GridViewEditEventArgs e)
        // {
            // GridView1.EditIndex = e.NewEditIndex;
            // BindData();
        // }

        // protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
        // {
            // GridView1.EditIndex = -1;
            // BindData();
        // }

        // protected void RowDataBound(object sender, GridViewRowEventArgs e)
        // {
            // if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
            // {
                // DropDownList ddlAssignees = (DropDownList)e.Row.FindControl("ddlAssignees");
                // string query = "Select an Assignee for this Request";
                // SqlCommand cmd = new SqlCommand(query);
                // ddlAssignees.DataSource = GetData(cmd);
                // ddlAssignees.DataTextField = "EmailAddress";
                // ddlAssignees.DataValueField = "Assignee";
                // ddlAssignees.DataBind();
                // ddlAssignees.Items.FindByValue((e.Row.FindControl("lblAssignee") as Label).Text).Selected = true;
            // }
        // }

        protected void Label_PreRender(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            DynamicFilter dynamicFilter = (DynamicFilter)label.FindControl("DynamicFilter");
            QueryableFilterUserControl fuc = dynamicFilter.FilterTemplate as QueryableFilterUserControl;
            if (fuc != null && fuc.FilterControl != null)
            {
                label.AssociatedControlID = fuc.FilterControl.GetUniqueIDRelativeTo(label);
            }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            RouteValueDictionary routeValues = new RouteValueDictionary(GridView1.GetDefaultValues());
            base.OnPreRenderComplete(e);
        }

        protected void DynamicFilter_FilterChanged(object sender, EventArgs e)
        {
            GridView1.PageIndex = 0;
        }

    }
}
